package com.dbh.alg.algorithm.hot100.矩阵;

/**
 * &#x7ED9;&#x5B9A;&#x4E00;&#x4E2A; n &times; n &#x7684;&#x4E8C;&#x7EF4;&#x77E9;&#x9635; matrix &#x8868;&#x793A;&#x4E00;&#x4E2A;&#x56FE;&#x50CF;&#x3002;&#x8BF7;&#x4F60;&#x5C06;&#x56FE;&#x50CF;&#x987A;&#x65F6;&#x9488;&#x65CB;&#x8F6C; 90 &#x5EA6;&#x3002;
 *
 * &#x4F60;&#x5FC5;&#x987B;&#x5728; &#x539F;&#x5730; &#x65CB;&#x8F6C;&#x56FE;&#x50CF;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4F60;&#x9700;&#x8981;&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x8F93;&#x5165;&#x7684;&#x4E8C;&#x7EF4;&#x77E9;&#x9635;&#x3002;&#x8BF7;&#x4E0D;&#x8981; &#x4F7F;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x77E9;&#x9635;&#x6765;&#x65CB;&#x8F6C;&#x56FE;&#x50CF;&#x3002;
 *
 *     1  2  3        7  4  1
 *     4  5  6   ==>  8  5  2
 *     7  8  9        9  6  3
 *
 * &#x8F93;&#x5165;&#xFF1A;matrix = [[1,2,3],[4,5,6],[7,8,9]]
 * &#x8F93;&#x51FA;&#xFF1A;[[7,4,1],[8,5,2],[9,6,3]]
 *
 *  matrix[row][col] => matrix[col][n-row-1]
 *
 *     5   1   9   11       15  13  2  5
 *     2   4   8   10  ==>  14  3   4  1
 *     13  3   6   7        12  6   8  9
 *     15  14  12  16       16  7  10  11
 *
 *
 *     row = col
 *                     ==>  matrix[n-row-1][n-col-1] = matrix[col][n-row-1]
 *     col = n-row-1
 *
 *                                                           temp = matrix[n-row-1][n-col-1]
 *     matrix[n-row-1][n-col-1] = matrix[col][n-row-1]  =>   matrix[n-row-1][n-col-1] = matrix[col][n-row-1]
 *                                                           matrix[col][n-row-1] = matrix[row][col]
 *
 *     row = n-row-1
 *                    ==>   matrix[n-col-1][row] = matrix[n-row-1][n-col-1]
 *     col = n-col-1
 *
 *      temp = matrix[n-col-1][row]
 *      matrix[n-col-1][row] = matrix[n-row-1][n-col-1];
 *      matrix[n-row-1][n-col-1] = matrix[col][n-row-1]
 *      matrix[col][n-row-1] = matrix[row][col]
 *
 *      row = n-col-1
 *                     ==>  matrix[row][col] = matrix[n-col-1][row]
 *      col = row
 *
 *       temp = matrix[row][col]
 *       matrix[row][col] = matrix[n-col-1][row]
 *       matrix[n-col-1][row] = matrix[n-row-1][n-col-1];
 *       matrix[n-row-1][n-col-1] = matrix[col][n-row-1]
 *       matrix[col][n-row-1] = temp
 *
 */
public class Leetcode48_旋转图像 {

    /**
     *       5   1   9   11       15  13  2  5
     *       2   4   8   10  ==>  14  3   4  1
     *      13  3   6   7        12  6   8  9
     *      15  14  12  16       16  7  10  11
     * @param matrix 数组
     */
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return;
        }
        int n = matrix.length;
        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < (n + 1) / 2; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n-j-1][i];
                matrix[n-j-1][i] =  matrix[n-i-1][n-j-1];
                matrix[n-i-1][n-j-1] =  matrix[j][n-i-1];
                matrix[j][n-i-1] = temp;
            }
        }
    }
}
